package com.dangf.hzapp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.dangf.hzapp.R;
import com.dangf.hzapp.fragment.MenuFragment;

/**
 * 主界面
 * 
 * @author 韦海生
 * @date 2014年8月2日
 */
public class MainActivity extends BaseActivity {
	public static DrawerLayout drawerLayout;		//抽屉
	public static Activity mainActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getActionBar().setDisplayHomeAsUpEnabled(false);		//不显示 返回上级按钮
		
		drawerLayout = (DrawerLayout) findViewById(R.id.main_drawerLayout);
		//设置菜单布局
		if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.left_frame, new MenuFragment()).commit();
        }
		// 关闭启动界面
		if (StartActivity.startActivity != null)StartActivity.startActivity.finish();
		if(mainActivity==null)mainActivity=this;
	}

	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {

		// 监听菜单键
		switch (keyCode) {
		case KeyEvent.KEYCODE_MENU:
			if (!drawerLayout.isDrawerOpen(Gravity.LEFT))drawerLayout.openDrawer(Gravity.LEFT);
			else drawerLayout.closeDrawer(Gravity.START);
			break;
		case KeyEvent.KEYCODE_BACK:
			drawerLayout.openDrawer(Gravity.LEFT);
			return true;
		}
		// 最后，一定要做完以后返回 true，或者在弹出菜单后返回true，其他键返回super，让其他键默认
		return super.onKeyUp(keyCode, event);
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			if("合租账簿".equals(getActivityName())){
				if(!drawerLayout.isDrawerOpen(Gravity.LEFT))drawerLayout.openDrawer(Gravity.LEFT);
				else drawerLayout.closeDrawer(Gravity.START);
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	

	@Override
	protected String getActivityName() {
		return "合租账簿";
	}

}
